package com.example.dangerouscargodriver.ui.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;

import com.baiju.netmanager.Http.IHttpRequest;
import com.example.dangerouscargodriver.R;
import com.example.dangerouscargodriver.adapter.UserGuideInfoAdapter;
import com.example.dangerouscargodriver.entry.CompanyInfo;
import com.example.dangerouscargodriver.entry.UserGuideInfo;
import com.example.dangerouscargodriver.module.ResponseInfo;
import com.example.dangerouscargodriver.param.RemoteAPI;
import com.example.dangerouscargodriver.param.RemoteAPICmd;
import com.example.dangerouscargodriver.widget.ActionBar;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;

public class UserGuideActivity extends HttpRequestActivity {

    private static final String TAG = "UserGuideActivity";

    private WebView mGuideView = null;
    private List<UserGuideInfo> mGuideInfo = null;
    private UserGuideInfoAdapter mAdapter = null;

    private RecyclerView mGuideInfoListView = null;
    private ActionBar mActionBar = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.initHttpComponent(UserGuideActivity.class);
        setContentView(R.layout.activity_user_guide);

        mGuideInfoListView = findViewById(R.id.rv_user_guide_info_list);
        mAdapter = new UserGuideInfoAdapter();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mGuideInfoListView.setLayoutManager(layoutManager);
        mGuideInfoListView.setAdapter(mAdapter);

        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.divider_1dp));
        mGuideInfoListView.addItemDecoration(divider);

        mActionBar = findViewById(R.id.actionBarUserGuide);
        mActionBar.setOnBackClickListener( new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                finish();
            }
        });

        executeGetAgreement();
    }

    private void executeGetAgreement(){
        HashMap<String, String> param = new HashMap<String, String>();

        sendHttpRequest(
                IHttpRequest.RequestMethod.GET_METHOD,
                RemoteAPI.REQUEST_V1_USER_GUIDE,
                null,
                param,
                RemoteAPICmd.REQUEST_V1_USER_GUIDE);
    }

    private void refreshUserGuideView(){
        mAdapter.clearData();
        mAdapter.appendData(mGuideInfo);
    }

    @Override
    protected void onHttpResponse(int actionId, String response) {
        try {
            JSONObject res = new JSONObject(response);
            Gson gson = new Gson();
            if(actionId == RemoteAPICmd.REQUEST_V1_USER_GUIDE){
                Log.d(TAG, "onResponse: " + response);
                ResponseInfo responseInfo = gson.fromJson(response, ResponseInfo.class);
                if(responseInfo != null && responseInfo.getStatus() == 1){
                    JSONArray data = res.getJSONObject("data").getJSONArray("list");
                    Type type = new TypeToken<List<UserGuideInfo>>() {}.getType();
                    mGuideInfo = gson.fromJson(data.toString(), type);
                    refreshUserGuideView();
                }
                if(responseInfo != null && responseInfo.getStatus() == 0){
                }
            }
            else{

            }
        }
        catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onHttpErrorResponse(int actionId, String response) {

    }
}